package com.testdemo;

import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;

/**
 * Auth：yujunyao
 * Since: 2016/5/19 15:39
 * Email：yujunyao@yonglibao.com
 */
public class CustomDialog extends Dialog {

    public CustomDialog(Context context) {
        super(context);
    }

    public CustomDialog(Context context, int themeResId) {
        super(context, themeResId);
    }

    public static class Builder {
        private Context context;
        private String title;
        private String content;
        private String leftBtnText;
        private String rightBtnText;
        private View.OnClickListener leftBtnListener;
        private View.OnClickListener rightBtnListener;

        public Builder(Context context) {
            this.context = context;
        }

        public Builder setTitle(String title) {
            this.title = title;
            return this;
        }

        public Builder setTitle(int titleID) {
            this.title = (String) context.getText(titleID);
            return this;
        }

        public Builder setContent(String content) {
            this.content = content;
            return this;
        }

        public Builder setContent(int contentID) {
            this.content = (String) context.getText(contentID);
            return this;
        }

        public Builder setLeftButton(String leftText, View.OnClickListener leftBtnListener) {
            this.leftBtnText = leftText;
            this.leftBtnListener = leftBtnListener;
            return this;
        }

        public Builder setLeftButton(int leftTextID, View.OnClickListener leftBtnListener) {
            this.leftBtnText = (String) context.getText(leftTextID);
            this.leftBtnListener = leftBtnListener;
            return this;
        }

        public Builder setRightButton(String rightText, View.OnClickListener rightBtnListener) {
            this.rightBtnText = rightText;
            this.rightBtnListener = rightBtnListener;
            return this;
        }

        public Builder setRightButton(int rightTextID, View.OnClickListener rightBtnListener) {
            this.rightBtnText = (String) context.getText(rightTextID);
            this.rightBtnListener = rightBtnListener;
            return this;
        }

        public void showDialog() {
            final CustomDialog dialog = new CustomDialog(context, R.style.MyDialogStyleCenterFuzzy);
            Window window = dialog.getWindow();
            window.setContentView(R.layout.dialog_global);
            TextView titleTv = (TextView) window.findViewById(R.id.dialog_global_title);
            titleTv.setText(title);

            TextView contentTv = (TextView) window.findViewById(R.id.dialog_global_content);
            contentTv.setText(content);

            if(!TextUtils.isEmpty(leftBtnText)) {
                Button leftBtn = (Button) window.findViewById(R.id.dialog_global_left);
                leftBtn.setText(leftBtnText);
                leftBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        leftBtnListener.onClick(v);
                    }
                });
            }

            if(!TextUtils.isEmpty(rightBtnText)) {
                Button rightBtn = (Button) window.findViewById(R.id.dialog_global_right);
                rightBtn.setText(rightBtnText);
                rightBtn.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        rightBtnListener.onClick(v);
                    }
                });
            }

            dialog.show();

        }

    }

}
